<?php

namespace app\admin\controller;

use app\admin\model\AdminLog;
use think\Config;
use think\Controller;
use think\Db;
use think\Hook;
use think\Validate;


class Info extends Controller
{


    public function _initialize()
    {
        parent::_initialize();
    }

    function test_1()
    {
//        $this->initUser();
//        $this->initVideo();
//        $this->initCourse();
//        $this->initLesson();
        $this->initOrder();
        echo "success";
    }

    protected function initUser()
    {
        $list = Db::connect('db_config1')->table('tp_user')->alias('u')
            ->join('tp_wechat_user w', 'w.open_id = u.open_id', 'left')
            ->where('u.org_id', $this->org_id)
            ->field('u.*,w.headimgurl')
            ->select();

        $user_db = Db::name("user");
        $third_db = Db::name("third");

        foreach ($list as $v) {
            echo json_encode($v) . "<br/>";

            $exist = $user_db->where('id', $v['id'])->find();
            if ($exist) continue;

            $exist = $third_db->where('openid', $v['open_id'])->find();
            if ($exist) continue;

            $user_db->insertGetId([
                'id' => $v['id'],
                'username' => 'u' . $v['id'],
                'nickname' => $v['real_name'],
                'mobile' => $v['mobile'],
                'avatar' => $v['headimgurl'],
                'password' => '',
                'email' => '',
            ]);

            $third_db->insertGetId([
                'user_id' => $v['id'],
                'platform' => 'wechat',
                'openid' => $v['open_id'],
                'openname' => $v['real_name'],
                'createtime' => $v['add_time'],
            ]);
        }
    }

    protected $org_id = 2;

    protected function initVideo()
    {
        $list = Db::connect('db_config1')->table('tp_video')
            ->where('org_id', $this->org_id)
            ->select();

        $video_db = Db::name('v_video');
        foreach ($list as $v) {
            $video_db->insert([
                'id' => $v['id'],
                'video_id' => $v['video_id'],
                'title' => $v['title'],
                'description' => $v['description'],
                'media_type' => $v['media_type'],
                'duration' => $v['duration'],
                'cover_image' => $v['cover_url'],
                'status' => $v['status'],
                'creation_time' => $v['creation_time'],
                'update_time' => $v['update_time'],
                'size' => $v['size'],
                'snapshots' => $v['snapshots'],
                'cate_id' => $v['cate_id'],
                'cate_name' => $v['cate_name'],
                'tags' => $v['tags'],
                'is_save' => $v['is_save'],
                'weight' => $v['weight'],
                'createtime' => time(),
            ]);
        }
    }

    protected function initCourse()
    {
        $list = Db::connect('db_config1')->table('tp_course')
            ->where('org_id', $this->org_id)
            ->select();

        $video_db = Db::name('v_course');
        foreach ($list as $v) {
            $exist = $video_db->where('id', $v['id'])->find();
            if ($exist) continue;

            $video_db->insert([
                'id' => $v['id'],
                'cover_image' => $v['cover_url'],
                'title' => $v['title'],
                'count' => $v['count'],
                'price' => $v['price'],
                'sort' => $v['sort'],
                'v_teacher_id' => $v['teacher_id'],
                'is_publish' => $v['is_publish'],
                'media_type' => $v['media_type'],
                'desc' => $v['desc'],
                'createtime' => $v['add_time'],
            ]);
        }
    }

    protected function initLesson()
    {
        $ids = Db::connect('db_config1')->table('tp_course')
            ->where('org_id', $this->org_id)
            ->column('id');

        $list = Db::connect('db_config1')->table('tp_lesson')
            ->where('course_id', 'in', $ids)
            ->select();

        $lesson_db = Db::name("v_lesson");
        foreach ($list as $v) {
            $exist = $lesson_db->where('id', $v['id'])->find();
            if ($exist) continue;

            $lesson_db->insert([
                'id' => $v['id'],
                'v_course_id' => $v['course_id'],
                'v_video_id' => $v['res_id'],
                'title' => $v['title'],
                'sort' => $v['sort'],
                'is_free' => $v['is_free'],
                'createtime' => $v['add_time'],
            ]);
        }
    }

    protected function initOrder()
    {
        $ids = Db::connect('db_config1')->table('tp_course')
            ->where('org_id', $this->org_id)
            ->where('is_publish', 1)
            ->column('id');

        $list = Db::connect('db_config1')->table('tp_user_order')
            ->where('course_id', 'in', $ids)
            ->select();

        $order_db = Db::name('v_user_order');
        foreach ($list as $v) {
            $exist = $order_db->where('order_id', $v['order_id'])->find();
            if ($exist) continue;

            $order_db->insert([
                'order_id' => $v['order_id'],
                'user_id' => $v['uid'],
                'v_course_id' => $v['course_id'],
                'way' => $v['way'],
                'money' => $v['money'],
                'state' => $v['state'],
                'is_read' => $v['is_read'],
                'createtime' => $v['add_time'],
            ]);
        }
    }
}
